import { VantMessage } from './vant-utils'
import Clipboard from 'clipboard'

/**
 *
 * @param {Event} event
 * @param {String} text
 */
const copyClipboard = (event, text) => {
  const copyText = text || event.currentTarget.getAttribute('data-clipboard-text')
  if (!copyText) {
    return
  }

  const clipboard = new Clipboard(event.currentTarget, {
    text: copyText
  })
  clipboard.on('success', () => {
    VantMessage.success('复制成功')
    clipboard.destroy()
  })
  clipboard.on('error', () => {
    VantMessage.error('复制失败')
    clipboard.destroy()
  })
  clipboard.onClick(event)
}

export default copyClipboard
